package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.mapper.SysRoleMapper;
import com.itheima.ydd.service.ISysMenuService;
import com.itheima.ydd.service.ISysRoleMenuService;
import com.itheima.ydd.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @Autowired
    private ISysMenuService sysMenuService;


    /**
     * 数据回显
     *
     * @param id
     * @return
     */
    public SysRoleDto getByCondition(String id) {
        SysRole sysRole = this.getById(id);
        SysRoleDto sysRoleDto = new SysRoleDto();
        BeanUtils.copyProperties(sysRole, sysRoleDto);
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId, id);
        List<SysRoleMenu> menuList = sysRoleMenuService.list(queryWrapper);
        List<SysMenu> sysMenuList = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : menuList) {
            LambdaQueryWrapper<SysMenu> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(SysMenu::getId, sysRoleMenu.getMenuId());
            SysMenu one = sysMenuService.getOne(queryWrapper1);
            sysMenuList.add(one);
        }

        List<SysMenu> sysMenus = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getPid() == 0) {
                sysMenus.add(sysMenu);
            }
        }

        List<SysMenuDto> sysMenuDtos = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            SysMenuDto sysMenuDto = new SysMenuDto();
            BeanUtils.copyProperties(sysMenu, sysMenuDto);
            LambdaQueryWrapper<SysMenu> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(SysMenu::getPid, sysMenu.getId());
            List<SysMenu> list = sysMenuService.list(queryWrapper2);
            ArrayList<SysMenuDto> sysMenuDtos1 = new ArrayList<>();
            for (SysMenu menu : list) {
                SysMenuDto sysMenuDto1 = new SysMenuDto();
                BeanUtils.copyProperties(menu, sysMenuDto1);
                ArrayList<SysMenu> menus = new ArrayList<>();
                for (SysMenu sysMenu1 : sysMenuList) {
                    if (sysMenu1.getType() == 2) {
                        menus.add(sysMenu1);
                    }
                }
                sysMenuDto1.setChildren(menus);
                sysMenuDtos1.add(sysMenuDto1);
            }
            sysMenuDto.setChildren(sysMenuDtos1);
            sysMenuDtos.add(sysMenuDto);
        }

        sysRoleDto.setAuthList(sysMenuDtos);
        return sysRoleDto;
    }

    /**
     * 角色添加
     *
     * @param sysRoleDto
     */
    @Transactional
    public void add(SysRoleDto sysRoleDto) {
        String id = UUID.randomUUID().toString();
        sysRoleDto.setId(id);
        sysRoleDto.setCreateDate(LocalDateTime.now());
        sysRoleDto.setUpdateDate(LocalDateTime.now());
        this.save(sysRoleDto);
        List<String> menuids = sysRoleDto.getMenuids();
        for (String menuid : menuids) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(id);
            sysRoleMenu.setMenuId(menuid);
            sysRoleMenuService.save(sysRoleMenu);
        }
    }
}
